﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TCPServer.script.db;
using TCPServer.script.net;
using TCPServer.script.proto;

namespace TCPServer
{
    internal class Program
    {
        //private static byte[] receiveBuffer = new byte[1024];

        private static void Main(string[] args)
        {
            //连接数据库
            if (!DBManager.Connect("wufu", "172.17.0.16", 3306, "root", "Li391320"))
            {
                return;
            }
            NetManager.StartLoop("172.17.0.16", 3913);
        }

        //private static void StartServerAsync()
        //{
        //    Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //    IPAddress address = IPAddress.Parse("192.168.1.143");
        //    IPEndPoint endPoint = new IPEndPoint(address, 3913);
        //    serverSocket.Bind(endPoint);
        //    serverSocket.Listen(0);//开启监听，设定最大的挂起队列长度,0为不限定。服务器只处理队列长度内的连接

        //    //处理一个客户端请求
        //    serverSocket.BeginAccept(AcceptCallBack, serverSocket);
        //}

        //private static void AcceptCallBack(IAsyncResult ar)
        //{
        //    Socket serverSocket = ar.AsyncState as Socket;
        //    Socket clientSocket = serverSocket.EndAccept(ar);
        //    //向客户端发送一条消息
        //    string msg = "Link success!";
        //    byte[] data = System.Text.Encoding.UTF8.GetBytes(msg);
        //    clientSocket.Send(data);

        //    //接收客户端一条消息
        //    clientSocket.BeginReceive(receiveBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);

        //    serverSocket.BeginAccept(AcceptCallBack, clientSocket);
        //}

        //private static void ReceiveCallBack(IAsyncResult ar)
        //{
        //    Socket clientSocket = ar.AsyncState as Socket;//获取BeginReceive中的state参数
        //    try
        //    {
        //        int count = clientSocket.EndReceive(ar);
        //        string msg = System.Text.Encoding.UTF8.GetString(receiveBuffer, 0, count);
        //        Console.WriteLine(msg);
        //        clientSocket.BeginReceive(receiveBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        if (clientSocket != null)
        //        {
        //            clientSocket.Close();
        //        }
        //    }
        //    finally
        //    {
        //        Console.WriteLine("finally");
        //    }
        //}
    }
}